package com.passive.lock.controller.route;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TRouteVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.route.RouteService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;

@Controller
@RequestMapping("ro")
public class RouteController {
	@Autowired
	private RouteService routeService;

	/**
	 * 线路信息管理
	 *
	 */

	@RequestMapping(value = "/routelist")
	@ResponseBody
	public Result routeList(@RequestParam Integer regionid, @RequestParam(required = false) Integer prouteid,
			@RequestParam(required = false) String routename, @RequestParam Integer page, @RequestParam Integer limit,
			HttpServletRequest req, HttpServletResponse res) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		if (regionid == null || "".equals(regionid)) {
			regionid = tUserVo.getRegionid();
		}

		try {
			int offset = PageUtil.getOffset(page, limit);
			List<TRouteVo> list = routeService.routeList(regionid, routename, tUserVo.getGroupid(), prouteid, offset,
					limit);
			int count = routeService.routeListCount(regionid, routename, tUserVo.getGroupid(), prouteid);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/addroute")
	@ResponseBody
	public Result addRoute(@RequestBody TRouteVo tRouteVo) {
		try {
			Result result = routeService.addRoute(tRouteVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/updateroute")
	@ResponseBody
	public Result updateRoute(@RequestBody TRouteVo tRouteVo) {
		try {
			Result result = routeService.updateRoute(tRouteVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteroute")
	@ResponseBody
	public Result deleteRoute(@RequestBody TRouteVo tRouteVo) {
		try {
			Result result = routeService.deleteRoute(tRouteVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/deleteroutelist")
	@ResponseBody
	public Result deleteRouteList(@RequestBody List<Integer> tRouteids) {
		try {
			Result result = routeService.deleteRouteList(tRouteids);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}

	@RequestMapping(value = "/queryroutelist")
	@ResponseBody
	public Result queryrouteList(@RequestParam(required = false) Integer prouteid,
			@RequestParam(required = false) Integer regionid, HttpServletRequest req, HttpServletResponse res) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		try {
			Result result = routeService.queryrouteList(tUserVo.getGroupid(),prouteid, regionid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
}
